/**
 * @description 新增、编辑岗位弹窗
 * @author 李艳
 */

import React, {Component} from 'react'
import {Form, Input, Modal} from 'antd';
import {positionCategoryConfig} from "../../../configs/CommonComponentsConfig";
import SearchTable from "../../../commons/components/SearchTable";

const FormItem = Form.Item;

class EditPositionModal extends Component {
  constructor(props) {
    super(props);
    this.state = {
      modalVisible: false,
      confirmLoading: false,
    }
  }

  handleClose = () => {
    this.props.form.resetFields();
  };

  componentDidMount() {
    this.props.onRef(this);

  }

  render() {
    const formItemLayout = {
      labelCol: {
        span: 8
      },
      wrapperCol: {
        span: 12
      },
    };

    const {confirmLoading, modalVisible, handleOk, handleCancel, defaultValue, selectedNode, isAdd} = this.props;
    const {getFieldDecorator} = this.props.form;
    let childId = "";
    let FormValue = defaultValue;
    let title = "编辑";
    if (isAdd) {//如果是修改
      title = "新增"
      FormValue = {}
      FormValue.organization = selectedNode
    }
    return (
      <div>
        <Modal title={title}
               visible={modalVisible}
               onOk={handleOk}
               onCancel={handleCancel}
               width={600}
               afterClose={this.handleClose}
               confirmLoading={confirmLoading}
               maskClosable={false}
        >
          <Form>
            <FormItem
              {...formItemLayout}
              label="组织机构"
            >
              {getFieldDecorator('organization.name', {
                initialValue: FormValue.organization ? FormValue.organization.name : "",
              })(
                <Input disabled/>
              )}
            </FormItem>
            <FormItem
              {...formItemLayout}
              label="岗位类别"
            >
              {getFieldDecorator('positionCategory.id', {
                initialValue: FormValue.positionCategory ? FormValue.positionCategory.id : "",
                rules: [{required: true, message: '请选择岗位类别!'}]
              })(
                <SearchTable config={positionCategoryConfig} params={{"quickSearchProperties":["name"]}}/>
              )}
            </FormItem>
            <FormItem
              {...formItemLayout}
              label="名称">
              {getFieldDecorator('name', {
                initialValue: FormValue.name ? FormValue.name : "",
                rules: [{required: true,
                  max:50, message: '请填写名称!（不超过50个字符）'}]
              })(
                <Input/>
              )}
            </FormItem>

            {/*以下为隐藏的formItem*/}
            <FormItem
              style={{display: "none"}}>
              {getFieldDecorator('id', {
                initialValue: FormValue.id ? FormValue.id : "",
              })(
                <Input disabled/>
              )}
            </FormItem>
            <FormItem
              style={{display: "none"}}>
              {getFieldDecorator('organization.id', {
                initialValue: FormValue.organization ? FormValue.organization.id : "",
              })(
                <Input disabled/>
              )}
            </FormItem>
          </Form>
        </Modal>
      </div>
    );
  }
}

EditPositionModal = Form.create()(EditPositionModal);
export default EditPositionModal;
